import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/game_subcription_item.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/widgets/network_load_image.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

import '../../global_easy_loading.dart';

class SubscriptionItemEntity {
  GameSubscriptionDataItem data;
  bool showDateTitle;
  bool recommend;
  SubscriptionItemEntity(
    this.data, {
    required this.showDateTitle,
    required this.recommend,
  });
}

class SubscriptionItemCard extends StatefulWidget {
  final GameSubscriptionDataItem entity;
  final bool showDateTitle;
  const SubscriptionItemCard(this.entity, {this.showDateTitle = false});

  @override
  _SubscriptionItemCardState createState() => _SubscriptionItemCardState();
}

class _SubscriptionItemCardState extends State<SubscriptionItemCard> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(15),
      margin: widget.showDateTitle
          ? EdgeInsets.only(top: 16, bottom: 10)
          : EdgeInsets.only(top: 5, bottom: 5),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(12)),
      ),
      child: Column(
        children: [
          Container(
            child: Row(
              children: [
                Container(
                  width: 28,
                  height: 28,
                  child: Stack(
                    children: [
                      Positioned(
                        child: Container(
                          width: 28,
                          height: 28,
                          child: NetworkLoadImage.loadNetworkImage(
                            //  CachedNetworkImage(
                            // imageUrl:
                            widget.entity.background,
                            fit: BoxFit.contain,
                          ),
                        ),
                        left: 0,
                      ),
                      Positioned(
                        child: Container(
                          width: 28,
                          height: 28,
                          child: NetworkLoadImage.loadNetworkImage(
                            //  CachedNetworkImage(
                            // imageUrl:
                            widget.entity.cover,
                            fit: BoxFit.cover,
                          ),
                        ),
                        left: 0,
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  width: 8,
                ),
                Text(
                  widget.entity.date,
                  style: TextStyle(
                    color: Colors.black.withOpacity(0.6),
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                SizedBox(
                  width: 4,
                ),
                Text(
                  widget.entity.time,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                Spacer(),
                Text(
                  widget.entity.statusName,
                  style: TextStyle(
                    color: Colors.black.withOpacity(0.4),
                    fontSize: 11,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 8,
          ),
          // Container(
          //   alignment: Alignment.centerLeft,
          //   child: Text(
          //     '${widget.entity.disciplineName}${widget.entity.scheduleName}',
          //     style: TextStyle(
          //       color: Colors.black.withOpacity(0.8),
          //       fontSize: 16,
          //       fontWeight: FontWeight.w500,
          //     ),
          //   ),
          // ),
          Container(
            alignment: Alignment.centerLeft,
            child: RichText(
              textAlign: TextAlign.left,
              text: TextSpan(
                children: [
                  WidgetSpan(
                      child: widget.entity.videoType != 0
                          ? Padding(
                              padding: EdgeInsets.only(right: 4, bottom: 2),
                              child: widget.entity.videoType == 1
                                  ? Image.asset(
                                      "assets/icons/game_event_live.gif",
                                      width: 14,
                                      height: 14)
                                  : Image.asset(
                                      "assets/icons/game_event_video_ic.png",
                                      width: 16,
                                      height: 14),
                            )
                          : Text('')),
                  TextSpan(
                    text:
                        "${widget.entity.disciplineName} ${widget.entity.scheduleName}",
                    style: (TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 16,
                      color: Colors.black,
                    )),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(
            height: 3,
          ),
          Container(
            child: Row(
              children: [
                Image.asset(
                  'assets/icons/schedule_location.png',
                  width: 10,
                ),
                SizedBox(
                  width: 4,
                ),
                Expanded(
                  child: Text(
                    widget.entity.venueName,
                    style: TextStyle(
                      color: Colors.black.withOpacity(0.4),
                      // fontWeight: FontWeight.w300,
                      fontSize: 12,
                    ),
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  ),
                ),
                SizedBox(
                  width: 12,
                ),
                if (widget.entity.status != 'FINISHED')
                  GestureDetector(
                    onTap: ClickUtils.debounce(() async {
                      if (!UserInfo.me.isLogin) {
                        RouteManager.push(
                          context,
                          RoutMapping.loginByPwd(),
                        );
                        return;
                      }
                      if (widget.entity.subscribed) {
                        try {
                          await API.shared.game
                              .cancelMatchSubscribe(widget.entity.id);
                          widget.entity.subscribed = false;
                          customShowToast(S.of(context).unsubscribe_succeeded);
                          setState(() {});
                        } catch (e) {
                          await UtilsOther.showCustomShowError(e);
                          // if (e is DioError) {
                          //   customShowToast(e.message);
                          // } else {
                          //   customShowToast(S.of(context).network_error);
                          // }
                        }
                      } else {
                        try {
                          await API.shared.game
                              .subscribeMatch(widget.entity.id);
                          widget.entity.subscribed = true;
                          customShowToast(S.of(context).subscribe_succeeded);
                          setState(() {});
                        } catch (e) {
                          await UtilsOther.showCustomShowError(e);
                          // if (e is DioError) {
                          //   customShowToast(e.message);
                          // } else {
                          //   customShowToast(S.of(context).network_error);
                          // }
                        }
                      }
                    }),
                    child: Container(
                      height: 28,
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(4)),
                        color: widget.entity.subscribed
                            ? Color(0xffF7D749).withOpacity(0.3)
                            : Color(0xffF7D749),
                      ),
                      child: Text(
                        widget.entity.subscribed
                            ? S().Subscribed
                            : S().Subscribe,
                        maxLines: 1,
                        style: TextStyle(
                          color: widget.entity.subscribed
                              ? Colors.black.withOpacity(0.6)
                              : Colors.black,
                          // fontWeight: FontWeight.w500,
                          fontSize: 12,
                        ),
                      ),
                    ),
                  )
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class SubscriptionItem extends StatelessWidget {
  final GameSubscriptionDataItem entity;
  final bool showDateTitle;
  final bool recommend;
  SubscriptionItem(this.entity, this.showDateTitle, this.recommend);

  Widget dateTitle() {
    if (!showDateTitle) {
      return Container();
    }
    return Container(
      alignment: Alignment.bottomLeft,
      child: Row(
        // crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Image.asset("assets/icons/event_dot.png", width: 15),
          SizedBox(
            width: 5,
          ),
          Text(
            entity.date,
            style: TextStyle(
              color: ThemeColor.colorBlack60,
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }

  Widget cardInfo() {
    return Container(
      child: IntrinsicHeight(
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(width: 7),
            if (!recommend)
              VerticalDivider(
                width: 0,
                thickness: 0.5,
              ),
            if (!recommend) SizedBox(width: 10),
            Expanded(
              child: SubscriptionItemCard(
                entity,
                showDateTitle: showDateTitle,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          dateTitle(),
          cardInfo(),
        ],
      ),
    );
  }
}
